package com.starlink.model.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.starlink.model.excel.MallBookExcelDTO;
import com.starlink.model.excel.MallBookSpecificationExcelDTO;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zhouyuhang01
 * @Date: 2024/6/9 12:15
 * @Description: MallBookListener
 * @Version 1.0.0
 */
public class MallBookSpecificationListener extends AnalysisEventListener<Object> {
    private List<MallBookSpecificationExcelDTO> dataList = new ArrayList<>();
    @Override
    public void invoke(Object object, AnalysisContext analysisContext) {
        if (!ObjectUtils.isEmpty(object)) {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
            String mallBooksId = jsonObject.getString("0");
            BigDecimal costPrice = jsonObject.getBigDecimal("1");
            BigDecimal price = jsonObject.getBigDecimal("2");
            BigDecimal vipPrice = jsonObject.getBigDecimal("3");
            String specification = jsonObject.getString("4");
            BigDecimal volume = jsonObject.getBigDecimal("5");
            BigDecimal weight = jsonObject.getBigDecimal("6");
            MallBookSpecificationExcelDTO mallBookSpecificationExcelDTO = new MallBookSpecificationExcelDTO();
            mallBookSpecificationExcelDTO.setMallBooksId(mallBooksId);
            mallBookSpecificationExcelDTO.setCostPrice(costPrice);
            mallBookSpecificationExcelDTO.setPrice(price);
            mallBookSpecificationExcelDTO.setVipPrice(vipPrice);
            mallBookSpecificationExcelDTO.setSpecification(specification);
            mallBookSpecificationExcelDTO.setVolume(volume);
            mallBookSpecificationExcelDTO.setWeight(weight);
            dataList.add(mallBookSpecificationExcelDTO);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
    public List<MallBookSpecificationExcelDTO> getDataList() {
        return dataList;
    }
}
